<?php


/**
 * Description of MenuController
 *
 * @author dliebel
 */

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Model\Menu;
use Application\Form\FormMenu;

class MenuController extends AbstractActionController {

    public $dbAdapter;

    public function indexAction() {
        $this->dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter');

        $menu = new Menu('menu', $this->dbAdapter);
        $datos = $menu->fetchAll(true);
     
       
        
        
        //establezca la página actual en lo que ha pasado en la cadena de consulta, o en 1 si no definido
        $datos->setCurrentPageNumber((int) $this->params()->fromQuery('page', 1));
        // establecer el número de elementos por página 10
        $datos->setItemCountPerPage(10);
        $mensaje = "";
        return new ViewModel(array("titulo" => "Lista de Menú", "mensaje" => $mensaje, "datos" => $datos));
    }

    public function addAction() {
        $form = new FormMenu("form");
        $form->get('ok')->setValue('GUARDAR');

        $request = $this->getRequest();
        if ($request->isPost()) {
            $this->dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter');

            $menu = new Menu('menu', $this->dbAdapter);
            $form->setInputFilter($menu->getInputFilter());
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $data = $menu->exchangeArray($form->getData());
                $menu->saveOrUpdate($data);


                return $this->redirect()->toUrl($this->getRequest()->getBaseUrl() . '/application/menu');
//                 return $this->redirect()->toRoute('menu');
            }
        }


        $valores = array
            (
            "titulo" => "Agregar Nuevo Menú",
            "form" => $form,
            'url' => $this->getRequest()->getBaseUrl()
        );

        return new ViewModel($valores);
    }

    public function editAction() {
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
            return $this->redirect()->toUrl($this->getRequest()->getBaseUrl() . '/application/menu/add');
        }

        $this->dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter');
        $menu = new Menu('menu', $this->dbAdapter);
        try {

            $menuData = $menu->getObjectByID($id);
        } catch (\Exception $ex) {
            return $this->redirect()->toUrl($this->getRequest()->getBaseUrl() . '/application/menu');
        }

        $form = new FormMenu("form");
        $form->bind($menuData);
        $form->get('ok')->setAttribute('value', 'GUARDAR');

        $request = $this->getRequest();
        if ($request->isPost()) {

            $form->setInputFilter($menu->getInputFilter());
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $data = $menu->exchangeArray($form->getData());
                $menu->saveOrUpdate($data,$id);


                return $this->redirect()->toUrl($this->getRequest()->getBaseUrl() . '/application/menu');
            }
        }
        $valores = array
            (
            "titulo" => "Editando Menú",
            "form" => $form,
            "id" => $id,
            'url' => $this->getRequest()->getBaseUrl()
        );

        return new ViewModel($valores);
    }

    public function verAction() {
        $this->dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter');
        $menu = new Menu('menu', $this->dbAdapter);

        $id = (int) $this->params()->fromRoute('id', 0);
        $valores = array
            (
            "titulo" => "Ver Menú",
            "datos" => $menu->getObjectByID($id)
        );
        return new ViewModel($valores);
    }

    public function deleteAction() {
        $this->dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter');
        $menu = new Menu('menu', $this->dbAdapter);

        $id = $this->params()->fromRoute('id', 0);
        $mensaje = "Se ha eliminado correctamente " + $id;

        $data = array
            (
//            "id"=>$this->id,
            "titulo" => "titulo nuevo",
            "descripcion" => "esta muy loco",
            "comando" => "comandoloco",
//            "menu_id"=>$this->menuPadreId 
        );

        $menu->delete(10);
//        if (!){  
//            
//            $mensaje= $menu->getMess();
//        }
        $valores = array
            (
            "mensaje" => $mensaje,
            "datos" => $datos = $menu->getObjects()
        );



        $view = new ViewModel($valores);
        $view->setTerminal(true); //esto es para que devuelba sin el layout sino como un partial
        return $view;
    }

}

